const {
    Keypair,
    Transaction,
    SystemProgram,
    LAMPORTS_PER_SOL,
    TransactionMessage,
    TransactionInstruction,
    SYSVAR_CLOCK_PUBKEY,
    SYSVAR_RENT_PUBKEY
} = require("@solana/web3.js");

const {
    getKeyPairUser1, getKeyPairUser2
} = require('./MyKeypair');

const config = require('./config');


const solChain = require('./SolChain');

const {transactionMessage }= require('./utils');

const run = async ()=> {
    let logging_program_keypair = require('./program_keypair/spl_example_sysvar-keypair');
    let secretKey = Uint8Array.from(logging_program_keypair);
    let programKeypair = Keypair.fromSecretKey(secretKey);

    let programID = programKeypair.publicKey;

    const fromKeypair = await getKeyPairUser1();
    console.log('fromKeypair.publicKey', fromKeypair.publicKey.toBase58());

    let clockAccount = {
        pubkey:SYSVAR_CLOCK_PUBKEY,
        isSigner:false,
        isWritable:false
    };
    let rentAccount = {
        pubkey: SYSVAR_RENT_PUBKEY,
        isSigner:false,
        isWritable:false
    }

    let instruction = new TransactionInstruction({

        keys:[clockAccount, rentAccount],
        programId:programID,
        data:Buffer.from("")

    });
    let latestBlockhash = await solChain.getLatestBlockhash();

    let payerPk = fromKeypair.publicKey;

    let tx = transactionMessage(payerPk, latestBlockhash.blockhash, [instruction]);

    console.log('tx: ', tx);


    let balance = await solChain.getBalance(fromKeypair.publicKey);
    console.log('from balance: ', balance);
    await tx.sign([fromKeypair]);
    let rawTx = tx.serialize();


    let ret = await solChain.sendRawTransaction(rawTx);
    console.log('ret: ', ret);
}
run();